*This file is used in replicating findings for "Foreign Aid Funnel? Placebo-Based Assessment 
*of Aid Flows to  Non-Permanent U.N. Security Council Members" by Evangeline Reynolds (University 
*of Illinois) and Matthew Winters (University of Illinois). 

*The model specification comes from Vreeland and Dreher 2014. 
*But the history of Security Council compositions are changed for
*random but plausible compositions.

*capture log close
set more off
version 11.0
drop _all
clear matrix
clear mata
set mem 2000m
set mat 800

log using ".\100CodeRecords\08_Vreeland_&_Dreher_2014_chapter5_W_Permutations.log" , replace

use ".\01VreelandAndDreher\VreelandAndDreherChapter5.dta", clear
joinby year code using "07_Ch5wPerm.dta"

****************************************************
*  example - write out code
***************************************************
local controls pariah war rgdpl2_ln polity2new milit_aid_ln ydumREP* cotrendregREP* egytrendREP 	
local SubsetGlobal hic~=1 & code~="CHN" & code~="RUS" & year>1959
local SubsetAfrica africa==1 & hic~=1 & code~="CHN" & code~="RUS" & year>1959

xtreg 	odadisUSA_ln unsc `controls' 	///
		if `SubsetGlobal', cluster(code) fe

xtreg 	odadisUSA_ln SCReassign1 `controls'	///
		if `SubsetGlobal', cluster(code) fe

xtreg 	odadisUSA_ln unsc0 unsc1 unsc2 `controls'	///
		if `SubsetGlobal', cluster(code) fe level (90)

xtreg 	odadisUSA_ln SCReassign1 `controls'	///
		if `SubsetAfrica', cluster(code) fe		
		
		
summarize odadisUSA_0 if unsc==1, meanonly 
scalar meanIn = r(mean)
summarize odadisUSA_0 if unsc==0, meanonly
scalar meanOut = r(mean)
scalar meandiff = meanIn-meanOut	
display meanIn 
display meanOut 
display meandiff
		
***************************************************

	gen SCPlacebo  = .	
	gen SC0Placebo = .
	gen SC1Placebo = .
	gen SC2Placebo = .			
	
tempname sim
postfile `sim' index outcome subset  ///
			   meandiff  estimate   se   lb   ub   tstat   pv  /* All UNSC */ ///  
                estimate0  se0  lb0  ub0  tstat0  pv0 /* Unimportant years UNSC */ ///
                estimate1  se1  lb1  ub1  tstat1  pv1 /* Somwhat important years UNSC */ /// 
                estimate2  se2  lb2  ub2  tstat2  pv2 /* Important years UNSC */ /// 
			   using  09_PermuteModelResults  , replace				

scalar placebo_counter=-1			   
			   	   
foreach x of varlist unsc SCReassign* {		/* Loops through true assignment and all placebos */ ///

display placebo_counter
scalar placebo_counter = placebo_counter+1	
			
		  replace SCPlacebo  = `x'
		  replace SC0Placebo = `x' * impyear0
		  replace SC1Placebo = `x' * impyear1
		  replace SC2Placebo = `x' * impyear2
				
local OutcomeVariables odadisUSA_ln odadisJPN_ln odadisDEU_ln odadisGBR_ln odadisFRA_ln /*5.1 & 5.2 "Bilateral" */ ///
                        aiddisIBRD_ln  aiddisIDA_ln  /* 5.3 "World Bank" */   ///
					   odadisUN_ln odadisWFP_ln odadisUNDP_ln odadisUNICEF_ln odadisUNHCR_ln odadisUNTA_ln /* 5.4 & 5.5 "UN" */
						   
scalar outcome_counter=0
						   
		foreach y of varlist `OutcomeVariables'{  /* Go through all outcome variables */

		  scalar outcome_counter = outcome_counter+1
		  scalar outcome=outcome_counter
		  scalar index = placebo_counter
		  
		  quietly{
			forvalues z = 1/2{ 	/* Go through once with Global, then Africa */
			
				if `z'==1 {
				scalar subset = 1
				}
				if `z'==2 {
				scalar subset = 2
				}
				
				if `z'==1{
						display "Model 1"
						*Straight unsc
						xtreg 	`y' SCPlacebo `controls'	///
								if `SubsetGlobal', cluster(code) fe
						}
				if `z'==2{		
						display "Model 2"		
						*Africa Subset - Straight unsc
						xtreg 	`y' SCPlacebo `controls'	///
								if `SubsetAfrica', cluster(code) fe		
						}
					
					summarize `y' if SCPlacebo==1, meanonly 
					scalar meanIn = r(mean)
					summarize `y' if SCPlacebo==0, meanonly
					scalar meanOut = r(mean)
					scalar meandiff = meanIn-meanOut
					
					scalar estimate = _b[SCPlacebo]
					scalar se = _se[SCPlacebo]
					scalar lb = estimate - se*invttail(e(df_r),.05/2)
					scalar ub = estimate + se*invttail(e(df_r),.05/2)
					scalar tstat = (_b[SCPlacebo]/_se[SCPlacebo])
					scalar NumberObservations = 100
					scalar pv = ttail(NumberObservations,abs(tstat))*2								
						
				if `z'==1{
						display "Model 3"
						*Partitioned - SC partitioned by importance
						xtreg 	`y' SC0Placebo SC1Placebo SC2Placebo `controls' 	///
								if `SubsetGlobal', cluster(code) fe level(90) 
				}
				if `z'==2{
						display "Model 4"
						*Africa Subset - SC partitioned by importance
						xtreg 	`y' SC0Placebo SC1Placebo SC2Placebo `controls'	///
								if `SubsetAfrica', cluster(code) fe level (90)
						}								

					scalar estimate0 = _b[SC0Placebo]
					scalar se0 = _se[SC0Placebo]
					scalar lb0 = estimate0 - se*invttail(e(df_r),.05/2)
					scalar ub0 = estimate0 + se*invttail(e(df_r),.05/2)
					scalar tstat0 = (_b[SC0Placebo]/_se[SC0Placebo])
					scalar NumberObservations = 100
					scalar pv0 = ttail(NumberObservations,abs(tstat0))*2
					
					scalar estimate1 = _b[SC1Placebo]
					scalar se1 = _se[SC1Placebo]
					scalar lb1 = estimate1 - se*invttail(e(df_r),.05/2)
					scalar ub1 = estimate1 + se*invttail(e(df_r),.05/2)
					scalar tstat1 = (_b[SC1Placebo]/_se[SC1Placebo])
					scalar NumberObservations = 100
					scalar pv1 = ttail(NumberObservations,abs(tstat1))*2
					
					scalar estimate2 = _b[SC2Placebo]
					scalar se2 = _se[SC2Placebo]
					scalar lb2 = estimate2 - se*invttail(e(df_r),.05/2)
					scalar ub2 = estimate2 + se*invttail(e(df_r),.05/2)
					scalar tstat2 = (_b[SC2Placebo]/_se[SC2Placebo])
					scalar NumberObservations = 100
					scalar pv2 = ttail(NumberObservations,abs(tstat2))*2	
					
					post `sim' (index) (outcome) (subset) ///
						   (meandiff) (estimate)  (se)  (lb)  (ub)  (tstat)  (pv) ///
				           (estimate0) (se0) (lb0) (ub0) (tstat0) (pv0) ///
				           (estimate1) (se1) (lb1) (ub1) (tstat1) (pv1) ///
				           (estimate2) (se2) (lb2) (ub2) (tstat2) (pv2) 			
				}			
		}
		}
}
				
postclose `sim'
use 09_PermuteModelResults, clear
		
				
label define subsetlbl  1 "Global" 2 "Africa"			
label values subset subsetlbl

label define outcomelbl 1 "odadisUSA_ln" 2 "odadisJPN_ln" 3 "odadisDEU_ln" 4 "odadisGBR_ln" 5 "odadisFRA_ln" /*5.1 & 5.2 "Bilateral" */ ///
                        6 "aiddisIBRD_ln" 7 "aiddisIDA_ln"  /* 5.3 "World Bank" */   ///
					    8 "odadisUN_ln" 9 "odadisWFP_ln" 10 "odadisUNDP_ln" 11 "odadisUNICEF_ln" 12 "odadisUNHCR_ln" 13 "odadisUNTA_ln"
label values outcome outcomelbl
	
gen permutation = index > 0	

gen PreSortIndex = _n
	
sort outcome subset permutation pv

save "09_PermuteModelResults.dta", replace
use "09_PermuteModelResults.dta", clear	
				
log close

